
(function(UI,Control){
	UI.Widget = {
		
	};
	
	// set defalut config
	var config = {
	};
	
	//define control flow
	var flow = UI.Widget.flow = {
		preInit:function(ctr,op){
			if(op.convertMode)
			{
				ctr.dom.addClass("ccay-widget-body").wrap("<div class='ccay-widget'></div>");
				ctr.dom = ctr.dom.parent();
				op._builded = true;
			}
			else
			{	
				ctr.dom.addClass("ccay-widget");
				//字段生成的时的方法委托
				op.handler.buildField = $.Callbacks();
			}
			op.headOpeartion || (op.headOpeartion = {});

			//加载widget组件
			UI.Widget.Commponents.fireAll("build",ctr, op);
			
		},
		initOp:function(op){
			var view = op.view;
			if(view)
			{
				op = $.extend({
					renderType: view.renderType,
					modal : view.modal
				},op);
			}
			else
			{//初始化View
				this.flow.initTmpl(op);
				op.view = Ccay.View.create({
					modal:op.modal,
					tmpl:op.tmpl,
					layout:op.layout,
					renderType:op.renderType,
					buildControlTag:op.buildControlTag
				});
				delete op.tmpl;
			}
		},
		//初始化模板
		initTmpl:function(op,tmpl){
			var fields = op.fields || op.modal.fields;
			if(fields){
				var tmpl = ["${=this.startLayout()}"],fnCheck = op.fnCheckField;
				//根据字段生成模板项
				$.each(fields,function(name,field){
					if(!fnCheck || fnCheck(name,field,op)) 
					{
						op.handler.buildField.fire(field,op);
						tmpl.push("${",op.modal.modalName,".",field.alias || field.field,"} ");
					}
				});
				tmpl.push("${=this.endLayout()}")
				op.tmpl = tmpl.join("");
			}
		},
		init:function(ctr,op){
			//html转换模式下，不用初始化
			if(op.convertMode)
				return;
			this.flow.initOp.call(this,op);
			if(op.ajax){
				//是否为modal的request
				if($.isFunction(op.ajax)){
					op._request = op.ajax;
					ajax = {};
				}
			}
		},
		build:function(ctr,op){
			var panel = ctr.dom,flow = this.flow;
			if(op.convertMode)
			{
				
				panel.prepend(flow.buildHead(ctr,op));
			}
			else
			{
				var arr = [];
				arr.push(flow.buildHead(ctr,op));
				op.body && arr.push(op.body);
				//arr.push(flow.buildFoot(ctr,op));
				panel.html(arr.join(""));
			}
		},
		buildHead:function(ctr,op,arr){
			return op.showHead !== false ? "<div class='ccay-widget-head'><span>"+ (op.title || "") + "</span><samp></samp></div>" : null;
		},
		buildFoot:function(ctr,op,arr){
			return null;
		},
		bindEvent:function(ctr,op){
		},
		initComplete:function(ctr,op){
		
		},
		initAjax:function(ctr,op,ajax){
			if(op._request)
			{
				//设置自定义的请求
				op.fnRequest = function(ctr,op){
					var param = op.fnBuildParam ? op.fnBuildParam(op.param) : op.param;
					if(param)
					{
						//对参数进行通用的设置
						if(!$.isPlainObject(param))
						{
							var key = param;
							param = {urlParam:{}};
							param.urlParam[op.modal.modalKey] = key;
						}
						param.success = op.ajax.success;
						op.modal.proxy(op._request,param);
					}
					else
					{
						op.ajax.success({});
					}
				}
			}
			else {
//				ajax.setAjax = function(ajax){
//					ajax
//				};
			}
		},
		preBindData:function(ctr,op,data,pageVO){
		
		},
		bindData:function(ctr,op,data){
			op.param && delete op.param;
			//判断是否编译完成，如果是则只做数据赋值操作
			if(!op._builded)
			{
				var body = ctr.getBody();
				op.view.render(body,op.renderType,data)
				op._builded = true;
			}
			else
			{
				ctr.setValue(data);
			}
		},
		bindDataComplete:function(ctr,op){
		
		},
		complete:function(ctr,op){
			if(!op._hideBody)
			{
				ctr.getBody().show();
				delete op._hideBody;
			}
			//表头进行事件绑定
			op.showHead !== false && ctr.getHeadBar().on('click','[eno]',function(e){
					var btn = $(this), eno = btn.attr("eno");
					if(eno){
						var ev = ctr.op.headOpeartion[eno];
						ev && ev.call(btn,e,ctr);
					}
				});
		}
	};
	
	var manager = {
		getHead:function(){
			return DomClass.getProp(this, this.dom, "pHead", ".ccay-widget-head");
		},
		getHeadBar:function(){
			return DomClass.getProp(this, this.getHead(), "pHeadBar", "samp");
		},
		getBody:function(){
			return DomClass.getProp(this, this.dom, "pBody", ".ccay-widget-body");
		},
		getFoot:function(){
			return DomClass.getProp(this, this.dom, "pBody", ".ccay-widget-foot");
		},
		valid:function(){
			return this.getBody().valid();
		},
		find:function(selector){
			return this.dom.find(selector);
		},
		findByName:function(name){
			return this.find('[name="'+ name + '"]');
		},
		getValue:function(type){
			return Ccay.Control.getValue(this.dom,this.op.modal,type == null ? "edit" : type);
		},
		setValue:function(data,type){
			Ccay.Control.setValue(this.dom,this.op.modal,data,type);
			return this;
		},
//		reload:function(param){
//			this.op.param = param;
//			this.base.reload.call(this,true);
//		},
		request:function(param){
			this.op.param = param;
			this.base.reload.call(this,true);
		},
		reBuild:function(){
			var data = this.getValue(),op = this.op,
				oldData = op.modal.getValue(),
				body = ctr.getBody();
			
			body.empty();
			flow.initTmpl(op);
			op.view.tmpl = op.tmpl;
			op.view.buildTmpl();
			op._builded = null;
			this.reload(data);
			op.modal.setData(oldData);
		}
	};
	//create XControl
	var widget = UI.XControl("widget",flow,config,manager,"Data");
	
	// create jquery plugin
	$.fn.widget = function(op){
		if(!UI.XControl.isInited(this))
			new widget (this, op);
		return this.manager("widget");
	}
	
	
})(Ccay.UI,Ccay.Control);
